package com.yuncheng.spcyApi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.entity.SpcyZltxwjHistory;
import com.yuncheng.spcyApi.mapper.SpcyZltxwjHistoryMapper;
import com.yuncheng.spcyApi.service.ISpcyZltxwjHistoryService;
import com.yuncheng.spcyApi.utils.LocalFileUploadUtils;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 审评查验-质量体系文件-历史库
 * @Author: yuncheng
 * @Date: 2023-07-04
 * @Version: V1.0
 */
@Api(tags = "审评查验-质量体系文件-历史库接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyZltxwjHistory")
public class SpcyZltxwjHistoryController {

    private static final Logger log = LoggerFactory.getLogger(SpcyZltxwjHistoryController.class);

    @Resource
    private ISpcyZltxwjHistoryService spcyZltxwjHistoryService;

    @Resource
    private SpcyZltxwjHistoryMapper spcyZltxwjHistoryMapper;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Log(title = "质量体系文件-历史库-分页列表查询")
    @ApiOperation(value = "质量体系文件-历史库-分页列表查询")
    @GetMapping(value = "/queryPageList")
    public HttpResult<?> queryPageList(SpcyZltxwjHistory bean,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyZltxwjHistory> page = new Page<>(pageNo, pageSize);

        if (StringUtils.isNotBlank(bean.getFtypeid())){
            bean.setFtype(null);
        }
        IPage<SpcyZltxwjHistory> pageList = spcyZltxwjHistoryMapper.selectPageList(page, bean);
        return HttpResult.ok(pageList);
    }

    @Log(title = "质量体系文件-历史库-通过id查询")
    @ApiOperation(value = "质量体系文件-历史库-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        SpcyZltxwjHistory spcyZltxwjBasic = spcyZltxwjHistoryService.getById(id);
        return HttpResult.ok(spcyZltxwjBasic);
    }

    @Log(title = "质量体系文件-下载文件")
    @ApiOperation(value = "质量体系文件-下载文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "质量体系文件id",required = true)
    })
    @GetMapping(value = "/getZltxwjFile")
    public void getZltxwjFile(@RequestParam("id") String fileId, HttpServletResponse response) {
        if (StringUtils.isEmpty(fileId)){
            log.error("质量体系文件id为空");
            return;
        }
        SpcyZltxwjHistory zltxwj = spcyZltxwjHistoryService.getById(fileId);
        if (zltxwj == null){
            log.error("质量体系文件id为-{}的文件记录不存在",fileId);
            return;
        }
        localFileUploadUtils.ejzLocalFileURlByZdy(zltxwj.getFwjlj(),response);
    }
}
